//axios 二次封装
import axios from 'axios'
import { useUserStore } from '@/store/modules/user'
//创建axios实例,基础路径
const request = axios.create({
    baseURL: import.meta.env.VITE_APP_API,
    timeout: 40000
})

//请求拦截器，config是请求配置对象
request.interceptors.request.use(config=>{
    const userStore = useUserStore();
    if (userStore.userInfo.token) {
        config.headers.token = userStore.userInfo.token;
    }
    return config
})

//响应拦截器，response是响应对象
request.interceptors.response.use(response=>{
    //如果响应成功，则返回数据
    return response.data
},error=>{
    return Promise.reject(error)
})

export default request


